package com.maowei.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.maowei.QuickCanalClientListener;
import com.maowei.event.EventHandlerFactory;
import com.maowei.parser.DefaultRowDtaCustomParser;
import com.maowei.parser.RowDataCustomParser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * canal 自动装配配置
 *
 * @author maowei
 * @date 2021/8/24
 */
@Configuration
@ConditionalOnProperty(name = "canal.enable", havingValue = "true")
@EnableConfigurationProperties(CanalConfigProperties.class)
@Import({CanalConnectorConfiguration.class})
public class CanalAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(RowDataCustomParser.class)
    public RowDataCustomParser rowDataCustomParser() {
        return new DefaultRowDtaCustomParser();
    }

    @Bean
    public EventHandlerFactory eventHandlerFactory() {
        return new EventHandlerFactory();
    }

    @Bean
    public QuickCanalClientListener quickCanalClientListener(CanalConnector canalConnector, CanalConfigProperties canalConfig,
                                                             EventHandlerFactory eventHandlerFactory) {
        return new QuickCanalClientListener(canalConnector, canalConfig, eventHandlerFactory);
    }
}
